/**
 * @FileName: QuestionTemplate.java
 * @Package: top.swimmer.demo.nlp
 * @author youshipeng
 * @created 2017/5/2 10:47
 * <p>
 * Copyright 2016 ziroom
 */
package top.swimmer.demo.nlp;

import com.hankcs.hanlp.seg.common.Term;
import top.swimmer.demo.entity.SentenceTrunk;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static top.swimmer.tools.str.StringUtils.isEmpty;

/**
 * <p></p>
 *
 * <PRE>
 * <BR>	修改记录
 * <BR>-----------------------------------------------
 * <BR>	修改日期			修改人			修改内容
 * </PRE>
 *
 * @author youshipeng
 * @since 1.0
 * @version 1.0
 */
public class QuestionTemplate {
    private static final Map<String, QuestionType> questionTemplate = new HashMap<String, QuestionType>() {{
        put(".*n_ziroom_maintain.*zbad_ziroom_maintain.*", QuestionType.MAINTAIN);
        put(".*zbad_ziroom_maintain.*n_ziroom_maintain.*", QuestionType.MAINTAIN);
        put(".*vn_ziroom_maintain.*n_ziroom_maintain.*", QuestionType.MAINTAIN_SURE);
        put(".*n_ziroom_maintain.*vn_ziroom_maintain.*", QuestionType.MAINTAIN_SURE);
        put(".*vn_ziroom_maintain.*", QuestionType.MAINTAIN_SURE);
        put(".*vreserve_ziroom_public.*vn_ziroom_maintain.*", QuestionType.MAINTAIN_SURE);

//        put(".*vreserve_ziroom_public.*v_ziroom_clean.*", "CLEAN_SURE");
//        put(".*vreserve_ziroom_public.*ntype_ziroom_clean.*", "CLEAN_SURE");
//        put(".*zbad_ziroom_clean.*n_ziroom_clean.*", "CLEAN");
//        put(".*n_ziroom_clean.*zbad_ziroom_clean.*", "CLEAN");
//        put(".*v_ziroom_clean.*", "CLEAN_SURE");
//        put(".*v_ziroom_clean.*n_ziroom_clean.*", "CLEAN_SURE");
//        put(".*n_ziroom_clean.*v_ziroom_clean.*", "CLEAN_SURE");
    }};

    public static QuestionType getQuestionType(SentenceTrunk sentenceTrunk) {
        String target = getQuestionTemplate(sentenceTrunk);
        if (isEmpty(target)) return null;
        for (Map.Entry<String, QuestionType> template : questionTemplate.entrySet()) {
            Pattern pat = Pattern.compile(template.getKey());
            Matcher matcher = pat.matcher(target);
            if (matcher.matches()) {
                return template.getValue();
            }
        }
        return null;
    }

    public static String getQuestionTemplate(SentenceTrunk sentenceTrunk) {
        List<Term> terms = SegmentUtils.seg(sentenceTrunk.toString());
        String template = "";
        for (Term term : terms) {
            template += "/";
            template += term.nature.name();
        }

        return template;
    }
}